//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.yls.except.util.json;

import java.util.ArrayList;

public class JsonTool {
    private static String PREFIX = "\\u";

    public JsonTool() {
    }

    public static String native2Ascii(String str) {
        char[] chars = str.toCharArray();
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < chars.length; ++i) {
            sb.append(char2Ascii(chars[i]));
        }

        return sb.toString();
    }

    private static String char2Ascii(char c) {
        if(c > 255) {
            StringBuilder sb = new StringBuilder();
            sb.append(PREFIX);
            int code = c >> 8;
            String tmp = Integer.toHexString(code);
            if(tmp.length() == 1) {
                sb.append("0");
            }

            sb.append(tmp);
            code = c & 255;
            tmp = Integer.toHexString(code);
            if(tmp.length() == 1) {
                sb.append("0");
            }

            sb.append(tmp);
            return sb.toString();
        } else {
            return Character.toString(c);
        }
    }

    public static String ascii2Native(String unicodeStr) {
        StringBuilder sb = new StringBuilder();
        int begin = 0;

        for(int index = unicodeStr.indexOf(PREFIX); index != -1; index = unicodeStr.indexOf(PREFIX, begin)) {
            sb.append(unicodeStr.substring(begin, index));
            sb.append(ascii2Char(unicodeStr.substring(index, index + 6)));
            begin = index + 6;
        }

        sb.append(unicodeStr.substring(begin));
        return sb.toString();
    }

    private static char ascii2Char(String unicodeStr) {
        if(unicodeStr.length() != 6) {
            throw new IllegalArgumentException("Ascii string of a native character must be 6 character.");
        } else if(!"\\u".equals(unicodeStr.substring(0, 2))) {
            throw new IllegalArgumentException("Ascii string of a native character must start with \"\\u\".");
        } else {
            String tmp = unicodeStr.substring(2, 4);
            int code = Integer.parseInt(tmp, 16) << 8;
            tmp = unicodeStr.substring(4, 6);
            code += Integer.parseInt(tmp, 16);
            return (char)code;
        }
    }

    public static String formatJson(String jsonString, String fillStringUnit) {
        if(jsonString != null && jsonString.trim().length() != 0) {
            ArrayList eachRowStringList = new ArrayList();

            String buf;
            for(String fixedLenth = jsonString; fixedLenth.length() > 0; fixedLenth = fixedLenth.substring(buf.length())) {
                buf = getEachRowOfJsonString(fixedLenth);
                eachRowStringList.add(buf.trim());
            }

            int var10 = 0;

            int i;
            for(int var11 = 0; var11 < eachRowStringList.size(); ++var11) {
                String count = (String)eachRowStringList.get(var11);
                i = count.getBytes().length;
                if(i > var10 && var11 < eachRowStringList.size() - 1 && ((String)eachRowStringList.get(var11 + 1)).equals(":")) {
                    var10 = i;
                }
            }

            StringBuilder var12 = new StringBuilder();
            int var13 = 0;

            for(i = 0; i < eachRowStringList.size(); ++i) {
                String token = (String)eachRowStringList.get(i);
                if(token.equals(",")) {
                    var12.append(token);
                    doFill(var12, var13, fillStringUnit);
                } else if(token.equals(":")) {
                    var12.append(" ").append(token).append(" ");
                } else {
                    String var14;
                    if(token.equals("{")) {
                        var14 = (String)eachRowStringList.get(i + 1);
                        if(var14.equals("}")) {
                            ++i;
                            var12.append("{ }");
                        } else {
                            ++var13;
                            var12.append(token);
                            doFill(var12, var13, fillStringUnit);
                        }
                    } else if(token.equals("}")) {
                        --var13;
                        doFill(var12, var13, fillStringUnit);
                        var12.append(token);
                    } else if(token.equals("[")) {
                        var14 = (String)eachRowStringList.get(i + 1);
                        if(var14.equals("]")) {
                            ++i;
                            var12.append("[ ]");
                        } else {
                            ++var13;
                            var12.append(token);
                            doFill(var12, var13, fillStringUnit);
                        }
                    } else if(token.equals("]")) {
                        --var13;
                        doFill(var12, var13, fillStringUnit);
                        var12.append(token);
                    } else {
                        var12.append(token);
                        if(i < eachRowStringList.size() - 1 && ((String)eachRowStringList.get(i + 1)).equals(":")) {
                            int fillLength = var10 - token.getBytes().length;
                            if(fillLength > 0) {
                                for(int j = 0; j < fillLength; ++j) {
                                    var12.append(" ");
                                }
                            }
                        }
                    }
                }
            }

            return var12.toString();
        } else {
            return null;
        }
    }

    private static String getEachRowOfJsonString(String jsonString) {
        StringBuilder buf = new StringBuilder();
        boolean isInYinHao = false;

        while(jsonString.length() > 0) {
            String firstString = jsonString.substring(0, 1);
            if(!isInYinHao && (firstString.equals(":") || firstString.equals("{") || firstString.equals("}") || firstString.equals("[") || firstString.equals("]") || firstString.equals(","))) {
                if(buf.toString().trim().length() == 0) {
                    buf.append(firstString);
                }
                break;
            }

            jsonString = jsonString.substring(1);
            if(firstString.equals("\\")) {
                buf.append(firstString);
                buf.append(jsonString.substring(0, 1));
                jsonString = jsonString.substring(1);
            } else if(firstString.equals("\"")) {
                buf.append(firstString);
                if(isInYinHao) {
                    break;
                }

                isInYinHao = true;
            } else {
                buf.append(firstString);
            }
        }

        return buf.toString();
    }

    private static void doFill(StringBuilder buf, int count, String fillStringUnit) {
        buf.append("\n");

        for(int i = 0; i < count; ++i) {
            buf.append(fillStringUnit);
        }

    }
}
